package com.cskaoyan.service;

import com.cskaoyan.bean.MallUser;
import com.cskaoyan.bean.MallUserExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.param.UserParam;
import com.cskaoyan.mapper.MallUserMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserMapper userMapper;

    @Autowired
    MallUserMapper mallUserMapper;

    @Override
    public BaseData<User> queryUser(UserParam param) {
        List<User> users = userMapper.selectUser(param);
        int total = userMapper.selectCounts(param);
        //BaseData<User> userBaseData = new BaseData<>();
        //userBaseData.setItems(users);
        //userBaseData.setTotal(total);
        //return userBaseData;
        return BaseData.ok(users, total);
    }
    @Override
    public BaseData<MallUser> queryUser2(UserParam param) {
        MallUserExample mallUserExample = new MallUserExample();
        //根据username和mobile是否为null构造查询条件
        //根据sort和order去写排序
        //里面包含List<Criterion> 条件列表
        MallUserExample.Criteria criteria = mallUserExample.createCriteria();
        if (param.getMobile() != null) {
            criteria.andMobileEqualTo(param.getMobile());
        }
        if (param.getUsername() != null) {
            criteria.andUsernameLike("%" + param.getUsername() + "%");
        }
        mallUserExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallUser> users = mallUserMapper.selectByExample(mallUserExample);

        int total = mallUserMapper.countByExample(mallUserExample);
        return BaseData.ok(users, total);
    }
    @Override
    public BaseData<MallUser> queryUser3(UserParam param) {
        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(),param.getLimit());

        MallUserExample mallUserExample = new MallUserExample();
        //根据username和mobile是否为null构造查询条件
        //根据sort和order去写排序
        //里面包含List<Criterion> 条件列表
        MallUserExample.Criteria criteria = mallUserExample.createCriteria();
        if (param.getMobile() != null) {
            criteria.andMobileEqualTo(param.getMobile());
        }
        if (param.getUsername() != null) {
            criteria.andUsernameLike("%" + param.getUsername() + "%");
        }
        mallUserExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallUser> users = mallUserMapper.selectByExample(mallUserExample);

        //total可以使用countByExample，也可以使用PageHelper来获得
        //int total = mallUserMapper.countByExample(mallUserExample);

        //在执行查询之后可以获得分页信息,将查询结果放入PageInfo的构造方法里
        PageInfo<MallUser> pageInfo = new PageInfo<>(users);
        int total = (int) pageInfo.getTotal();


        return BaseData.ok(users, total);
    }
}
